using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using System.Windows.Forms;
using System.Drawing;

namespace BodySoundSimulator.AbsoluteMapData
{
    class AbsoluteBodyFeaturesDataManipulator
    {
        public static readonly Pen ribCageDrawingPen = new Pen(Color.Black);

        public static AbsoluteBodyFeatures LoadAbsoluteBodyFeatureData()
        {
            AbsoluteBodyFeatures data = null;
            StreamReader reader = null;
            try
            {
                reader = new StreamReader(Path.Combine(Application.StartupPath, "RibCageData.xml"));
                XmlSerializer deserializer = new XmlSerializer(typeof(AbsoluteBodyFeatures));
                data = deserializer.Deserialize(reader) as AbsoluteBodyFeatures;
            }
            catch
            {
                data = new AbsoluteBodyFeatures();
                data.FillDummyInfo();
                SaveRibCageData(data);
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }
            return data;
        }

        public static void SaveRibCageData(AbsoluteBodyFeatures data)
        {
            if (data == null)
            {
                data = new AbsoluteBodyFeatures();
            }
            StreamWriter streamWriter = null;
            try
            {
                streamWriter = new StreamWriter(Path.Combine(Application.StartupPath, "RibCageData.xml"));
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(AbsoluteBodyFeatures));
                xmlSerializer.Serialize(streamWriter, data);
            }
            catch (System.IO.IOException)
            {
            }
            finally
            {
                if (streamWriter != null)
                {
                    streamWriter.Close();
                }
            }
        }

        public static void DrawFrontView(Graphics g, AbsoluteBodyFeatures data)
        {
            g.DrawLine(ribCageDrawingPen, data.JugularNotch, data.ChinMargin);
            g.DrawLine(ribCageDrawingPen, data.JugularNotch, data.CavicleEndRight);
            g.DrawLine(ribCageDrawingPen, data.JugularNotch, data.CavicleEndLeft);
            DrawRib(g, data.RibOneLeft);
            DrawRib(g, data.RibOneRight);
            DrawRib(g, data.RibTwoLeft);
            DrawRib(g, data.RibTwoRight);
            DrawRib(g, data.RibThreeLeft);
            DrawRib(g, data.RibThreeRight);
            DrawRib(g, data.RibFourLeft);
            DrawRib(g, data.RibFourRight);
            DrawRib(g, data.RibFiveLeft);
            DrawRib(g, data.RibFiveRight);
            DrawRib(g, data.RibSixLeft);
            DrawRib(g, data.RibSixRight);
            DrawRib(g, data.RibSevenLeft);
            DrawRib(g, data.RibSevenRight);
        }

        public static void DrawBackView(Graphics g, AbsoluteBodyFeatures data)
        {
            g.DrawLine(ribCageDrawingPen, data.JugularNotch, data.ChinMargin);
            g.DrawLine(ribCageDrawingPen, data.JugularNotch, data.CavicleEndRight);
            g.DrawLine(ribCageDrawingPen, data.JugularNotch, data.CavicleEndLeft);
            g.DrawLine(ribCageDrawingPen, data.JugularNotch, data.SpineEnd);
        }

        private static void DrawRib(Graphics g, RibInfo rib)
        {
            g.DrawLine(ribCageDrawingPen, rib.Point0, rib.Point1);
            g.DrawLine(ribCageDrawingPen, rib.Point1, rib.Point2);
            g.DrawLine(ribCageDrawingPen, rib.Point3, rib.Point4);
            g.DrawLine(ribCageDrawingPen, rib.Point4, rib.Point5);
            g.DrawLine(ribCageDrawingPen, rib.Point0, rib.Point3);
            g.DrawLine(ribCageDrawingPen, rib.Point2, rib.Point5);
        }

    }
}
